/**
 * 题目：
 * 字符串出现的不重复最长长度
 */

interface res {
  substring: string,
  length: number
}
/**
 * 数组方式
 */
export function getSubStringOflongest(str:string):res {
  let originArray:Array<string> = str.split('');
  let subArray:Array<string> = [];
  let length:number = 0;
  let substring:string = "";
  while(originArray.length) {
    let oItem = originArray.shift();
    let _index = subArray.indexOf((oItem as string));
    if(_index>=0){
      if(subArray.length>length){
        length = subArray.length;
        substring = subArray.join('');
      }
      subArray = subArray.slice(_index+1); 
    }
    subArray.push((oItem as string));
  }
  if(subArray.length>length){
    length = subArray.length;
    substring = subArray.join('');
  }
  return {
    substring,
    length
  }
}
/**
 * 字符串方式
 */
export function getSubStringOflongest2(str:string): res {
  let substring:string = "";
  let res:string = '';
  let i = 0;
  while(i < str.length) {
    let oItem = str.substr(i, 1);
    let _index = substring.indexOf((oItem as string));
    if(_index>=0){
      if(substring.length > res.length) {
        res = substring;
      }
      substring = substring.slice(_index+1); 
    }
    substring += oItem;
    i++;
  }
  if(substring.length > res.length) {
    res = substring;
  }
  return {
    substring:res,
    length: res.length
  }
}

/**
 * 
 */
export function getSubStringOflongest3(str:string): number{
  let res = 0;
  let i = -1;
  let map = new Map();
  for(let n = 0, m = str.length ; n<m; n++){
    if(map.has(str[n])){
      i = Math.max(i, map.get(str[n]));
    }
    res = Math.max(res, n-i);
    map.set(str[n], n);
  }
  return res
}